package com.jlboot.model.po.sys;

import com.jlboot.core.base.Models;
import com.jfinal.plugin.activerecord.Model;
import com.jlboot.core.cache.SysConfigCache;
import com.jlboot.core.consts.Consts;
import com.jlboot.core.util.LoginUtil;

import java.util.Date;

/**
 * Created by 51594 on 2022/7/27.
 */
public class SysConfig extends Model<SysConfig> {
    public static final SysConfig dao = new SysConfig().dao();
    public static final String table="sys_config";
    public Models mod = Models.n(this);

    public static String getNextId(){
        return SysIdGenerator.dao.getNextId(table);
    }

    public SysConfig setCommon(){
        Date now=new Date();
        this.set("id",SysIdGenerator.dao.getNextId(table));
        this.set("del_tag", Consts.COMMON_NONE_DEL);
        this.set("create_time",now);
        this.set("update_time",now);
        this.set("create_by", LoginUtil.getUserId());
        this.set("update_by",LoginUtil.getUserId());
        return this;
    }

    /**
     * 保存配置值
     *
     * @param cf_key
     * @param cf_value
     * @return
     */
    public boolean saveConfigValue(String cf_key, String cf_value) {
        SysConfig config = SysConfig.dao.findById(cf_key);
        if (config != null) {
            config.set("cf_value", cf_value).update();
            SysConfigCache.reload();
            return true;
        }
        return false;
    }

    /**
     * 取得系统配置值
     *
     * @param cf_key
     * @return
     */
    public String getConfigValue(String cf_key, String def_val) {
        SysConfig config = SysConfig.dao.findById(cf_key);
        if (config != null) {
            return config.mod.getStr("cf_value", def_val);
        }
        return def_val;
    }

    /**
     * 取得系统配置值
     *
     * @param cf_key
     * @return
     */
    public String getConfigValue(String cf_key) {
        return getConfigValue(cf_key, null);
    }

}
